package org.fintrace.core.drivers.tspl.controller;
import org.fintrace.core.drivers.tspl.commands.label.*;
import org.fintrace.core.drivers.tspl.commands.system.*;
import org.fintrace.core.drivers.tspl.connection.EthernetConnectionClient;
import org.fintrace.core.drivers.tspl.connection.TSPLConnectionClient;
import org.fintrace.core.drivers.tspl.model.MessageModel;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Log4j2
@RestController
public class EthernetController {

    @PostMapping("sendMessage")
    public void sendMessage(@RequestBody MessageModel model) throws InterruptedException {
        TSPLConnectionClient connectionClient = new EthernetConnectionClient(model.getPrinterIP(), 9100);
        try {
            connectionClient.init();
            connectionClient.connect();
            int count = 1;
            while (!connectionClient.isConnected()) {
                if(count > 10) {
                    throw new RuntimeException("打印机连接错误");
                } 
                log.info("Waiting for connection to Printer: " + model.getPrinterIP());
                Thread.sleep(1000);
                count++;
            }
            log.info("connection is OK to Printer: " + model.getPrinterIP());

            TSPLLabel tsplLabel = null;
            log.info("打印模板使用: " + model.getTemplateno()); 
            log.info("二维码: " + model.getBarcodeno()); 
            log.info("客 户:"+model.getAccount()); 
            log.info("合同号:"+model.getOrderno()); 
            log.info("部件1:"+model.getType1()); 
            log.info("部件1:"+model.getLengths1()); 
            log.info("部件2:"+model.getType2()); 
            log.info("部件2:"+model.getLengths2()); 
            log.info("部件3:"+model.getType3()); 
            log.info("部件3:"+model.getLengths3()); 
            log.info("毛重:"+model.getGrossweight()); 
            log.info("净重:"+model.getNetweight()); 
            log.info("包装方式:"+model.getOthers1()); 
            log.info("交货期:"+model.getOthers2()); 


            if("#1".equals(model.getTemplateno())){  //必须使用#1
                tsplLabel = TSPLLabel.builder()
                        .element(Size.builder().labelWidth(80f).labelLength(50f).build())
                        .element(Gap.builder().labelDistance(3f).labelOffsetDistance(0f).build())
                        .element(Direction.builder().printPositionAsFeed(Boolean.TRUE).build())
                        .element(ClearBuffer.builder().build())
                        .element(QRCode.builder().xCoordinate(1060).yCoordinate(140).errorCorrectionLevel(ErrorCorrectionLevel.M)
                                .cellWidth(4).mode(QREncodeMode.A).rotation(BarcodeRotation.NO_ROTATION).model(QRModel.M2)
                                .content(model.getBarcodeno()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(50).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content("客 户:"+model.getAccount()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(80).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content("合同号:"+model.getOrderno()).build())
                        .element(Text.builder().xCoordinate(1060).yCoordinate(50).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getOthers1()).build())
                        .element(Text.builder().xCoordinate(1060).yCoordinate(80).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getOthers2()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(110).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content("---------------------------").build())                        
                        .element(Text.builder().xCoordinate(50).yCoordinate(140).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getType1()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(170).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getLengths1()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(200).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content("---------------------------").build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(230).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getType2()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(260).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getLengths2()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(290).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content("---------------------------").build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(320).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getType3()).build())
                        .element(Text.builder().xCoordinate(50).yCoordinate(350).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getLengths3()).build())                        
                        .element(Text.builder().xCoordinate(1060).yCoordinate(320).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getGrossweight()).build())
                        .element(Text.builder().xCoordinate(1060).yCoordinate(350).fontName("FONT001")
                                .rotation(BarcodeRotation.NO_ROTATION).xMultiplicationFactor(2f).yMultiplicationFactor(2f)
                                .alignment(BarcodeAlignment.LEFT)
                                .content(model.getNetweight()).build())                        
                        .element(Print.builder().nbLabels(1).nbCopies(1).build())
                        .build();
            }           
            connectionClient.send(tsplLabel);
        }catch (Exception e) {
            log.error(e.getMessage());
            e.printStackTrace();
        }finally {
            connectionClient.disconnect();
            connectionClient.shutdown();
        }
    }
}
